package ldh.im.fxbase.dao;

import ldh.im.fxbase.data.pojo.ImModule;
import ldh.im.fxbase.data.pojo.ext.ImModuleExt;
import ldh.im.fxbase.util.DbUtil;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class ImModuleDao extends BaseDao {

    private static final String MODULE_COLUMNS = "id, name, description, no, status statusValue, createTime, updateTime";

    public void insert(ImModule imModule) throws SQLException {
        DbUtil.transaction((queryRunner, connection) -> {
            ResultSetHandler<Long> resultSetHandler = new ScalarHandler();
            String sql = String.format("insert into im_module(name, description, no, status, createTime, updateTime) values(?, ?, ?, ?, ?, ?)");
            Long id = queryRunner.insert(connection, sql, resultSetHandler, imModule.getName(), imModule.getDescription(), imModule.getNo(),
                    valueEnum(imModule.getStatus()), imModule.getCreateTime(), imModule.getUpdateTime());
            imModule.setId(id);
            return imModule;
        });
    }

    public void batchInserts(List<ImModule> imModules) throws SQLException {
        if (imModules.size() < 1) return;
        Object[][] params = imModules.stream().map(imModule-> new Object[]{imModule.getName(), imModule.getDescription(), imModule.getNo(),
                valueEnum(imModule.getStatus()), imModule.getCreateTime(), imModule.getUpdateTime()}).toArray(Object[][]::new);
        DbUtil.transaction((queryRunner, connection) -> {
            ColumnListHandler<Long> resultSetHandler = new ColumnListHandler();
            String sql = String.format("insert into im_module(name, description, no, status, createTime, updateTime) values(?, ?, ?, ?, ?, ?)");
            List<Long> ids = queryRunner.insertBatch(connection, sql, resultSetHandler, params);
            int idx = 0;
            for(Long id : ids) {
                imModules.get(idx++).setId(id);
            }
            return ids.size();
        });
    }

    public void batchUpdates(List<ImModule> imModules) throws SQLException {
        if (imModules.size() < 1) return;
        Object[][] params = imModules.stream().map(imModule-> new Object[]{imModule.getDescription(), valueEnum(imModule.getStatus()), imModule.getUpdateTime(),
                imModule.getId()}).toArray(Object[][]::new);

        DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("update im_module set description = ?,  status =?, updateTime = ? where id = ? ");
            int[] ns = queryRunner.batch(connection, sql, params);
            if (ns.length != imModules.size()) {
                throw new RuntimeException("更新数据失败");
            }
            return ns.length;
        });
    }

    public ImModule getByName(String name) throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select %s from im_module where name = ? ", MODULE_COLUMNS);
            ImModule imLabelExt = queryRunner.query(connection, sql, new BeanHandler<>(ImModuleExt.class), name);
            return imLabelExt;
        });
    }

    public List<ImModule> getAll() throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select %s from im_module where status = 1 ", MODULE_COLUMNS);
            List<ImModule> imLabelExts = queryRunner.query(connection, sql, new BeanListHandler<>(ImModuleExt.class));
            return imLabelExts;
        });
    }

}
